#!/usr/bin/env ruby

unless (ARGV[2]) then
  puts "usage :\n #{$0} fun3d.sol wolf.sol Mach"
  exit 1
end

mach = ARGV[2].to_f
gamma = 1.4

printf("Mach %f gamma %f\n",mach,gamma)

lines = IO.readlines(ARGV[0])

File.open(ARGV[1],'w') do |f|
  f.puts "MeshVersionFormatted 2"
  f.puts "Dimension 3"
  f.puts "SolAtVertices"
  nnode = lines[5].to_i
  printf("%d nodes\n",nnode)
  f.puts nnode
  f.puts "4 1 2 1 1"
  nnode.times do |i|
    line = lines[i+7]
    col = line.split()
    rho = col[0].to_f
    u = col[1].to_f
    v = col[2].to_f
    w = col[3].to_f
    p = col[4].to_f
    sa = col[5].to_f
    u /= mach
    v /= mach
    w /= mach
    p /= (mach*mach)
    e = 1.0/(gamma-1.0)*p + 0.5 * rho * (u*u+v*v+w*w)
# primitive
    f.printf( " %.18e %.18e %.18e %.18e %.18e %.18e\n", rho, u, v, w, p, sa )
#    f.printf( " %.18e %.18e %.18e %.18e %.18e\n",
#              rho, rho*u, rho*v, rho*w, e )
  end
  f.puts "End"
end

